import { unInstance } from '@/service'
import { useAuthStore } from '@/store'

interface RESPONSE {
  status: string
  message: string
  data: any
}

export default {
  /* 商户静默登录 */
  loginAndQuery(code: string) {
    return unInstance.post<RESPONSE>(`/auth/loginAndQuery`, {
      code,
      source: 'MT_MP',
    })
  },
  /* 验证手机号商户绑定关系并获取动态码/验证码 */
  getDynamicCode(data: any) {
    const { openid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/getDynamicCode`, Object.assign(data, {
      openid: openid.value,
      source: 'MT_MP',
    }))
  },
  /* 商户登录密码设置 */
  setLoginPassword(code: string, password: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/setLoginPassword`, {
      sessionid: sessionid.value,
      verificationCode: code,
      newPassword: password
    })
  },
  /* MP0056 忘记密码获取短信验证码 */
  forgetSendSMS(phoneNumber: string, merchantId: string) {
    const { openid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/forgetSendSMS`, {
      source: 'MT_MP',
      openid: openid.value,
      phoneNumber,
      merchantId
    })
  },
  /* MP0072 忘记密码验证短信并获取会话信息 */
  forgetVerify(data: any) {
    const { openid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/forgetVerify`, Object.assign(data, {
      source: 'MT_MP',
      openid: openid.value
    }))
  },
  /* MP0054 通过会话获取短信验证码 */
  getCodeBySession() {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/getCodeBySession`, {
      sessionid: sessionid.value
    })
  },
  /* MP0073 商户登录密码修改 */
  modifyPassword(data: any) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/modifyPassword`, Object.assign(data, {
      sessionid: sessionid.value
    }))
  },
  /* MP0055 忘记密码验证短信并获取会话信息并设置登录密码 */
  forgetVerifyAndSetPasswd(data: any) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/forgetVerifyAndSetPasswd`, Object.assign(data, {
      sessionid: sessionid.value
    }))
  },
  /* 商户退出登录 */
  logout(phoneNumber: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/logout`, {
      sessionid: sessionid.value,
      phoneNumber
    })
  },
  /* token刷新接口 */
  tokenRefresh(code: string) {
    return unInstance.post<RESPONSE>(`/auth/tokenRefresh`, {
      code,
      source: 'MT_MP',
    })
  },
  /* 强制修改密码 */
  forceModifyPassword(password: string) {
    const { sessionid } = storeToRefs(useAuthStore())
    return unInstance.post<RESPONSE>(`/auth/forceModifyPassword`, {
      sessionid: sessionid.value,
      newPassword: password
    })
  },
}
